/**
 * 
 */
package it.uniroma3.engine.dictionary;

import com.aliasi.spell.EditDistance;
import com.aliasi.spell.JaccardDistance;
import com.aliasi.spell.WeightedEditDistance;
import com.aliasi.tokenizer.IndoEuropeanTokenizerFactory;
import com.aliasi.tokenizer.TokenizerFactory;

/**
 * TODO DistancesImplementations : generate comments
 * 
 * @author Accosta Augusto
 * @author Lopez Elisabetta
 *
 */
public class DistancesImplementations {

	public static float NO_DISTANCE = 50f; // [= 0]
	public static float NOT_MUCH_DISTANCE = 10f; // [>1 <5]
	public static float SO_FAR_DISTANCE = 0f; // [> 5]
	
	
	/**
	 * Method that calculates the Edit distance
	 * 
	 * @param inputText
	 * @param proteinName
	 * @return the distance 
	 */
	public static double editDistance(String inputText, String proteinName) {
		EditDistance distance = new EditDistance(true);
		return distance.distance(inputText, proteinName);
	}
	
	/**
	 * Method that calculates the Weighted Edit distance
	 * 
	 * @param inputText
	 * @param proteinName
	 * @return the distance 
	 */
	public static double weightedEditDistance(String inputText, String proteinName) {
		
		WeightedEditDistance distance = new WeightedEditDistance() {
			
			@Override
			public double transposeWeight(char arg0, char arg1) {
				return Double.NEGATIVE_INFINITY;
			}
			
			@Override
			public double substituteWeight(char arg0, char arg1) {
				return ((Character.toString(arg0).equals("-") && Character.toString(arg1).equals(" ")) ||
						(Character.isDigit(arg0) && Character.isDigit(arg1)) ||
						(Character.isLowerCase(arg0) && Character.isUpperCase(arg1)))
						? -1d
						: -5d;
			}
			
			@Override
			public double matchWeight(char arg0) {
				return 0;
			}
			
			@Override
			public double insertWeight(char arg0) {
				return (Character.toString(arg0).equals(" ") || Character.toString(arg0).equals("-"))
						? -1d 
						: -10d;
			}
			
			@Override
			public double deleteWeight(char arg0) {
				return (Character.toString(arg0).equals(" ") || Character.toString(arg0).equals("-"))
						? -1d 
						: -10d;
			}
		};
		
		return distance.distance(inputText, proteinName);
		
	}
	
	/**
	 *  Method that calculates the Jaccard distance
	 *  
	 * @param inputText
	 * @param proteinName
	 * @return the distance
	 */
	public static double jaccardDistance(String inputText, String proteinName) {
		 TokenizerFactory tokenizerFactory = IndoEuropeanTokenizerFactory.INSTANCE;
	     JaccardDistance jaccard = new JaccardDistance(tokenizerFactory);
	     
	     return jaccard.distance(inputText, proteinName); 
	}
	
}
